#include <stdio.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include "select.h"
#include "player.h"

#include <json-c/json.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/resource.h>
#include <errno.h>
#include "device.h"
#include "socket.h"
#include <linux/input.h>

extern int g_maxFd;
extern fd_set readFds;

extern int g_buttonfd;

extern int g_sockfd;

extern int g_start_flag;
extern int g_suspend_flag;

extern long new_vol_left;
extern long new_vol_right;

extern shm *shmaddr;

//解析json数据
void parse_message(const char* name,char* c)
{
        struct json_object* obj=json_tokener_parse(name);

        struct json_object* json;
        json_object_object_get_ex(obj,"cmd",&json);
        strcpy(c,json_object_get_string(json));
}

//标准输入的控制菜单
void show()
{
        printf("1、开始播放\n");
        printf("2、结束播放\n");
        printf("3、暂停播放\n");
        printf("4、继续播放\n");
        printf("5、上一首\n");
        printf("6、下一首\n");
        printf("7、增加音量\n");
        printf("8、减小音量\n");
        printf("9、顺序播放\n");
        printf("a、随机播放\n");
        printf("b、单曲循环\n");

}


void my_select()
{

        show();

        fd_set tmpFds;
        FD_ZERO(&tmpFds);

        char message[1024]={0};

        FD_SET(g_buttonfd,&readFds);

        //要监听的最大文件描述符
        if(g_buttonfd > g_maxFd){
                g_maxFd=g_buttonfd;
        }
        
        while(1)
        {
                int ret;
                tmpFds=readFds;
                memset(message,0,sizeof(message));
                
                //printf("g_maxFd = %d\n",g_maxFd);

                ret=select(g_maxFd+1,&tmpFds,NULL,NULL,NULL);
                if(ret == -1 && errno != EINTR){
                        perror("select");
                }
                
                if(FD_ISSET(g_sockfd,&tmpFds)){              //TCP有数据可读

                        ret=recv(g_sockfd,message,sizeof(message),0);

                        printf("收到服务器消息：%s\n",message);

                        char cmd[64]={0};
                        parse_message(message,cmd);
                        if(!strcmp(cmd,"start"))
                        {
                                socket_start_play();
                        }else if(!strcmp(cmd,"stop"))
                        {
                                socket_stop_play();
                        }else if(!strcmp(cmd,"suspend"))
                        {
                                socket_suspend_play();        
                        }else if(!strcmp(cmd,"continue"))
                        {
                                socket_continue_play();
                        }else if(!strcmp(cmd,"prior"))
                        {
                                socket_prior_play();
                        }else if(!strcmp(cmd,"next"))
                        {
                                socket_next_play();
                        }else if(!strcmp(cmd,"voice_up"))
                        {
                                socket_voice_up_play();
                        }else if(!strcmp(cmd,"voice_down"))
                        {
                                socket_voice_down_play();
                        }else if(!strcmp(cmd,"sequence"))
                        {
                                socket_mode_play(SEQUENCEMODE);
                        }else if(!strcmp(cmd,"random"))
                        {
                                socket_mode_play(RANDOM);
                        }else if(!strcmp(cmd,"circle"))
                        {
                                socket_mode_play(CIRCLE);
                        }else if(!strcmp(cmd,"get"))//获取播放器状态
                        {           
                                socket_get_status();
                        }else if(!strcmp(cmd,"music"))//获取所有音乐
                        {         
                                socket_get_music();
                        }


                }
                else if(FD_ISSET(g_buttonfd,&tmpFds))           //按键有数据可读
                {          
                        int id = get_key_id();

                        if(id==0)
                        {
                                start_play();
                                continue;
                        }else if(id==1)
                        {
                                stop_play();
                                continue;
                        }
                        else
                        {
                                continue;
                        }
                }
        }
        
}

void socket_start_play()
{
        start_play();

        //回复操作成功
        struct json_object* json=json_object_new_object();
        json_object_object_add(json,"cmd",json_object_new_string("reply"));
        json_object_object_add(json,"result",json_object_new_string("start_success"));

        const char* buf=json_object_to_json_string(json);
        int ret=send(g_sockfd,buf,strlen(buf),0);
        if(-1==ret){
                perror("send");
        }
        printf("回复服务器：%s\n",buf);
}
void socket_stop_play()
{
        stop_play();

        //回复操作成功
        struct json_object* json=json_object_new_object();
        json_object_object_add(json,"cmd",json_object_new_string("reply"));
        json_object_object_add(json,"result",json_object_new_string("stop_success"));

        const char* buf=json_object_to_json_string(json);
        int ret=send(g_sockfd,buf,strlen(buf),0);
        if(-1==ret){
                perror("send");
        }
}
void socket_suspend_play()
{
        suspend_play();

        //回复操作成功
        struct json_object* json=json_object_new_object();
        json_object_object_add(json,"cmd",json_object_new_string("reply"));
        json_object_object_add(json,"result",json_object_new_string("suspend_success"));

        const char* buf=json_object_to_json_string(json);
        int ret=send(g_sockfd,buf,strlen(buf),0);
        if(-1==ret){
                perror("send");
        }
}
void socket_continue_play()
{
        continue_play();

        //回复操作成功
        struct json_object* json=json_object_new_object();
        json_object_object_add(json,"cmd",json_object_new_string("reply"));
        json_object_object_add(json,"result",json_object_new_string("continue_success"));

        const char* buf=json_object_to_json_string(json);
        int ret=send(g_sockfd,buf,strlen(buf),0);
        if(-1==ret){
                perror("send");
        }
}
void socket_prior_play()
{
        prior_play();

        //回复操作成功
        struct json_object* json=json_object_new_object();
        json_object_object_add(json,"cmd",json_object_new_string("reply"));
        json_object_object_add(json,"result",json_object_new_string("success"));

        const char* buf=json_object_to_json_string(json);
        int ret=send(g_sockfd,buf,strlen(buf),0);
        if(-1==ret){
                perror("send");
        }
}
void socket_next_play()
{
        next_play();

        //回复操作成功
        struct json_object* json=json_object_new_object();
        json_object_object_add(json,"cmd",json_object_new_string("reply"));
        json_object_object_add(json,"result",json_object_new_string("success"));

        const char* buf=json_object_to_json_string(json);
        int ret=send(g_sockfd,buf,strlen(buf),0);
        if(-1==ret){
                perror("send");
        }
}

void socket_voice_up_play()
{
        voice_up();

        //回复操作成功
        struct json_object* json=json_object_new_object();
        json_object_object_add(json,"cmd",json_object_new_string("reply"));
        json_object_object_add(json,"result",json_object_new_string("success"));

        const char* buf=json_object_to_json_string(json);
        int ret=send(g_sockfd,buf,strlen(buf),0);
        if(-1==ret){
                perror("send");
        }
}void socket_voice_down_play()
{
        voice_down();

        //回复操作成功
        struct json_object* json=json_object_new_object();
        json_object_object_add(json,"cmd",json_object_new_string("reply"));
        json_object_object_add(json,"result",json_object_new_string("success"));

        const char* buf=json_object_to_json_string(json);
        int ret=send(g_sockfd,buf,strlen(buf),0);
        if(-1==ret){
                perror("send");
        }
}

void socket_mode_play(int mode)
{
        set_mode(mode);
        
        //回复操作成功
        struct json_object* json=json_object_new_object();
        json_object_object_add(json,"cmd",json_object_new_string("reply"));
        json_object_object_add(json,"result",json_object_new_string("success"));

        const char* buf=json_object_to_json_string(json);
        int ret=send(g_sockfd,buf,strlen(buf),0);
        if(-1==ret){
                perror("send");
        }
}
